<?php

class Classe {

	private $nomClasse;
	private $primaryKeys = array();
	private $attributes = array();
	private $foreignKeys = array();
	private $hasError = false;
	private $messageError;
	private $exportInForm = false;

	public function __construct() {
	}

	public function getNomClasse(){
		return $this->nomClasse;
	}
	public function setNomClasse($nomClasse){
		$this->nomClasse = $nomClasse;
	}

	public function getPrimaryKeys(){
		return $this->primaryKeys;
	}
	public function setPrimaryKeys($primaryKeys){
		$this->primaryKeys = $primaryKeys;
	}
	
	public function addPrimaryKey($key, $primaryKey){
		$this->primaryKeys[$key] = $primaryKey;
	}

	public function getAttributes(){
		return $this->attributes;
	}
	public function setAttributes($attr){
		$this->attributes = $attr;
	}

	public function addAttribute($key, $value){
		$this->attributes[$key] = $value;
	}
	public function getAttribute($key){
		return $this->attribute[$key];
	}

	public function getForeignKeys(){
		return $this->foreignKey;
	}
	public function setForeignKeys($attr){
		$this->foreignKey = $attr;
	}

	public function addForeignKey($key, $value){
		$this->foreignKey[$key] = $value;
	}
	public function getForeignKey($key){
		return $this->foreignKey[$key];
	}

	public function getHasError(){
		return $this->hasError;
	}
	public function setHasError($hasError){
		$this->hasError = $hasError;
	}

	public function getMessageError(){
		return $this->messageError;
	}
	public function setMessageError($messageError){
		$this->messageError = $messageError;
	}
	
	public function setExportInForm($exportInForm){
		$this->exportInForm = $exportInForm;
	}
	public function getExportInForm(){
		return $this->exportInForm;
	}

	public function getQueryForCreate(){
		$query ="CREATE TABLE IF NOT EXISTS `".$this->nomClasse."` ( ";
		$allAttributes = $this->attributes;
		foreach ($allAttributes as $attribute){
			$query .= " `".$attribute->getName()."` ";
			if($attribute->getTypeSize() == "" || $attribute->getTypeSize() == 0 ){
				$query .= " ".$attribute->getType();
			}else{
				$query .= " ".$attribute->getType()."(".$attribute->getTypeSize().") ";
			}
		
			if($attribute->getPossibleValue() != null && $attribute->getPossibleValue() != "" ){
				$tabValues = explode(",",$attribute->getPossibleValue());
				$allValues = "'".implode($tabValues,"','")."'";
				$query .= " (".$allValues.")";
			}
			
			if($attribute->getIsNull() == "false"){
				$query .= " NOT NULL";
			}else{
				$query .= " NULL";
			}
			
			if($attribute->getIsAutoincrement() == "true"){
				$query .= " auto_increment";
			}	
			
			if($attribute->getComment() != ""){
				$query .= " COMMENT '".$attribute->getComment()."'";
			}
			
			$query .=", ";
		}
		
		$query .=" PRIMARY KEY (`".implode($this->primaryKeys,'`,`')."`)";
			
		$allForeignKeys = $this->getForeignKeys();
		if(sizeof($allForeignKeys) > 0){
			foreach ( array_keys($allForeignKeys) as $key){
				$query .=", FOREIGN KEY ".$allForeignKeys[$key]." ";
			}
		}
		
		$query .=") ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;";
		
		/*
		$allAttributes = $this->attributes;
		foreach ( array_keys($allAttributes) as $key){
			$query .=" `".$key."` ";
			$param = array();
			$param = $allAttributes[$key];
			foreach ($param as $p){
				$query .=" ".$p." ";
			}
			$query .=", ";
		}
		$query .=" PRIMARY KEY (`".$this->primaryKey."`)";
			
		$allForeignKeys = $this->getForeignKeys();
		if(sizeof($allForeignKeys) > 0){
			foreach ( array_keys($allForeignKeys) as $key){
				$query .=", FOREIGN KEY ".$allForeignKeys[$key]." ";
			}
		}
		
		$query .=") ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;";
		*/
		echo "<br><br><br>".$query."<br><br><br>";
		return $query;
		}
	
}